<?php

/**
 * Description of epoca
 *
 * @author Leonardo Lino
 */
class Epoca extends CI_Controller {

    private $smarty;

    public function __construct() {
        parent::__construct();
        require_once 'smartyLoader.php';
        $this->smarty = $smarty;
        $this->load->model('epocaModel');
        $this->user->isAccessBackOffice($this->smarty);
    }

    public function index() {

        $epocas = $this->epocaModel->loadAll();
        if ($epocas == null) {
            $this->smarty->assign("vazio", 0);
        } else {
            $this->smarty->assign("vazio", 1);
        }
        

        $this->smarty->assign("admins", $this->user->getAdmins());
        $this->smarty->assign("epocas", $epocas);
        $this->smarty->assign("templateActivo", "epoca.tpl.html");
        $this->smarty->display('admin/header.tpl.html');
        $this->smarty->display('admin/main.tpl.html');
        $this->smarty->display('admin/footer.tpl.html');
    }
    
    public function delete (){
      $resultado = $this->epocaModel->deleteFromId($_GET['vid']);
      $this->index();
    }

    public function novo() {

        $state = $_POST['state'];
        if ($state == "1") {
            $resultado = $this->epocaModel->novo($_POST['id_autor'], $_POST['data_ini'], $_POST['data_fim'], $_POST['nome']);
            $this->index();
        } else {
            //($id, $id_autor, $data_ini, $data_fim, $nome)
            $resultado= $this->epocaModel->update($_POST['vid'], $_POST['id_autor'], $_POST['data_ini'], $_POST['data_fim'], $_POST['nome']);
            $this->index();
        }
    }



    public function loadAjax() {
        $row = $this->epocaModel->loadFromId($_POST['vid']);
        echo json_encode($row);
    }

    public function encodeJsonSingle($row) {
        //print json_encode( array( 1 => 'a',  2 => 'b', 3 => 'c' ) ) ;
        foreach ($row as $key => $value) {
            $data[$key] = $value;
        }
        return $data;
    }

}

?>
